package com.jn.erp.web.api.student.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.course.entity.vo.DataTrendsVo;
import com.jn.erp.web.api.student.entity.vo.EnrollmentData;
import com.jn.erp.web.api.student.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

// 招生数据
@RestController
@RequestMapping("/enrollmentData")
public class EnrollmentDataController extends BaseWebAuthController {


    @Autowired
    private StudentService studentService;

    /**
     * 招生数据列表
     * 所有或者新增线索列表
     * @param dataTrendsVo
     * @return
     */
    @GetMapping("rankingList")
    public TableDataInfo rankingList(DataTrendsVo dataTrendsVo) {
        startPage();
        dataTrendsVo.setOrgId(Long.parseLong(loginDeptAuth()));
        List<EnrollmentData> list = studentService.rankingList(dataTrendsVo);
        return getDataTable(list);
    }

    /**
     *所有或者新增线索数
     * @param dataTrendsVo
     * @return
     */

    @PostMapping("studentDataNum")
    public AjaxResult clueDataNum(@RequestBody DataTrendsVo dataTrendsVo) {
        dataTrendsVo.setOrgId(Long.parseLong(loginDeptAuth()));
        Map list = studentService.clueDataNum(dataTrendsVo);
        return AjaxResult.success(list);
    }

    /**
     * 数据趋势
     * @param dataTrendsVo
     * @return
     */
    @PostMapping("dataTrendsCharts")
    public AjaxResult dataTrendsCharts(@RequestBody DataTrendsVo dataTrendsVo) {
        //机构id
        dataTrendsVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        dataTrendsVo.setCreateUser(String.valueOf(getUserId()) );
        Map<String, List<List>> dataTrends = studentService.dataTrendsEChart(dataTrendsVo);
        return AjaxResult.success(dataTrends);
    }


    @RequestMapping("/exportdataTrend")
    public void exportdataTrend(@RequestBody DataTrendsVo dataTrendsVo, HttpServletResponse response){
        //机构id
        dataTrendsVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        dataTrendsVo.setCreateUser(String.valueOf(getUserId()) );
        Workbook sheets = null;
        if(StringUtils.isEmpty(dataTrendsVo.getExportType())){
            dataTrendsVo.setExportType("1");
        }
        //3.返回表格
        //设置表格文件名字
        String fileName = "招生数据明细";
        //1趋势概况2上课老师报表3班级报表4课程报表5在读学员报表
                //1.查询数据
                 List<EnrollmentData> list = studentService.rankingList(dataTrendsVo);
                AtomicInteger i = new AtomicInteger(1);

                //2.封装成表格
                //参数1：表格标题，参数2：sheet名称
                ExportParams exportParams = new ExportParams(null, "招生数据明细");
                //参数1：表格参数  参数2：实体类  参数3：数据
                sheets = ExcelExportUtil.exportExcel(exportParams, EnrollmentData.class, list);
                fileName = "招生数据明细";
        try {
            fileName = URLEncoder.encode(fileName, "UTF8");
            //设置返回数据类型
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");

            //将表格输出
            OutputStream os = response.getOutputStream();
            sheets.write(os);
            os.close();
            os.flush();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
